<?php defined('SYSPATH') or die('No direct script access.');

// Статьи
class Controller_Admin_Pages extends Controller_Admin_Base
{
    /*
    * Выводим все с возможностью редактирования
    */
	public function action_index()
	{        
        // Запросы на удаление страниц
        if ($this->request->method() == HTTP_Request::POST)
        {            
            $action = $this->request->post('action');
            
            if($action == 'delete_pages')
            {                
                $pages = $this->request->post('pages') ? $this->request->post('pages') : null;
                
                if($pages)
                {
                    foreach($pages as $page_id)
                    {
                        $this->page_model->delete_page($page_id);
                    }
                    
                    $message = 'Удалили'; 
                }
            }
        }
        
        $pages = $this->page_model->get_pages();
        
		$title = 'Страницы';
        $content = new View('admin/pages/index_tpl');
        $content->pages = $pages;
        
        
        $menu = array(
            'menu'    => 4,
            'submenu' => 42
        );
        
        $this->print_template($title, $content, $menu);
	}
	
    /*
    * Добавление, ПОСТ запрос прилетает сюда же
    * редирктим на index
    */
	public function action_add()
	{        
        if ($this->request->method() == HTTP_Request::POST)
        {            
            $title = $this->request->post('title') ? trim($this->request->post('title')) : null;
            $url   = $this->request->post('url') ? trim($this->request->post('url')) : null;
            $text  = $this->request->post('text') ? trim($this->request->post('text')) : null;
            $description = $this->request->post('description') ? trim($this->request->post('description')) : null;
            $sort  = $this->request->post('sort') ? (int)$this->request->post('sort') : null;                      
            
            if($title && $url && $text)
            {                
                $page_id = $this->page_model->add_page($title, $url, $text, $sort, $description);
                
                Request::current()->redirect('admin/pages');
            }
        }
        
        $title = 'Добавление Страницы';
        $sort = $this->page_model->get_max_sort() + 1; 
        
        $content = new View('admin/pages/add_tpl');
        $content->sort = $sort;
                
        $menu = array(
            'menu'    => 4,
            'submenu' => 41
        );
        
        $this->print_template($title, $content, $menu);
	}
	
    /*
    * Редактирование, потом редиректим на реферрер
    */
	public function action_edit()
	{
        $page_id = $this->request->param('id');
        
        $message = null;

        if ($this->request->method() == HTTP_Request::POST)
        {            
            $title = $this->request->post('title') ? trim($this->request->post('title')) : null;
            $url   = $this->request->post('url') ? trim($this->request->post('url')) : null;
            $text  = $this->request->post('text') ? trim($this->request->post('text')) : null;
            $description = $this->request->post('description') ? trim($this->request->post('description')) : null;
            $sort  = $this->request->post('sort') ? (int)$this->request->post('sort') : null;
            
            if($title && $url && $text)
            {                
                $this->page_model->update_page($title, $url, $text, $sort, $description, $page_id);
                
                $message = 'Отредактировали успешно!';
                
                //Request::current()->redirect(Request::referrer());
            }
        }
        
        $page = $this->page_model->get_page($page_id);
        
		$title = $page['title'].' - Редактирование';
        
        $content = new View('admin/pages/edit_tpl');
        $content->page    = $page;
        $content->message = $message;
        
        $menu = array(
            'menu'    => 4,
            'submenu' => 40
        );
        
        $this->print_template($title, $content, $menu);
	}
    
    /*
    * Удаление, потом редиректим на реферрер
    */
    public function action_delete()
    {
        $page_id = $this->request->param('id');
        
        if($page_id)
            $this->page_model->delete_page($page_id);
            
        Request::current()->redirect($this->request->referrer());
    }

} // End Admin_Pages
